package com.jiguangssp.addemo.adapter;

import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.jiguangssp.addemo.R;
import com.jiguangssp.addemo.entity.AdData;

import java.util.List;

import xyz.adscope.amps.ad.nativead.adapter.AMPSNativeAdExpressListener;
import xyz.adscope.amps.ad.nativead.inter.AMPSNativeAdExpressInfo;
import xyz.adscope.amps.ad.unified.inter.AMPSUnifiedNativeItem;
import xyz.adscope.amps.ad.unified.inter.AMPSUnifiedPattern;
import xyz.adscope.amps.ad.unified.view.AMPSUnifiedRootContainer;

public class NativeListAdapter extends RecyclerView.Adapter<NativeListAdapter.Holder> {
    private List<AdData> list;

    public NativeListAdapter(List<AdData> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new Holder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_native_list, parent, false));
    }

    private static final String TAG = "JgPuAdNativeAdapter!";

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {
        AdData adData = list.get(holder.getLayoutPosition());
        holder.textView.setText(adData.getContent());

        if (adData.getAdInfo() != null) {
            Object adObject = adData.getAdInfo();
            if (adObject instanceof AMPSUnifiedNativeItem) {
                AMPSUnifiedNativeItem nativeItem = (AMPSUnifiedNativeItem) adObject;
                if (nativeItem.isExpressAd()) {
                    nativeItem.setNativeAdExpressListener(new AMPSNativeAdExpressListener() {
                        @Override
                        public void onAdShow() {
                            Log.e(TAG, "onAdShow: ");
                        }

                        @Override
                        public void onAdClicked() {
                            Log.e(TAG, "onAdClicked: ");
                        }

                        @Override
                        public void onAdClosed(View view) {
                            Log.e(TAG, "onAdClosed: ");
                            list.remove(holder.getLayoutPosition());
                            notifyItemRemoved(holder.getLayoutPosition());
                        }

                        @Override
                        public void onRenderFail(View view, String s, int i) {
                            Log.e(TAG, "onRenderFail: ");
                        }

                        @Override
                        public void onRenderSuccess(View view, float v, float v1) {
                            Log.e(TAG, "onRenderSuccess: ");
                            ((ViewGroup) holder.itemView).addView(view);

                        }
                    });
                    nativeItem.render();
                }
            } else if (adObject instanceof AMPSNativeAdExpressInfo) {
                Log.e(TAG, "list adapter: ");
                AMPSNativeAdExpressInfo expressInfo = ((AMPSNativeAdExpressInfo) adObject);
                expressInfo.setAMPSNativeAdExpressListener(new AMPSNativeAdExpressListener() {
                    @Override
                    public void onAdShow() {
                        Log.e(TAG, ":onAdShow ");
                    }

                    @Override
                    public void onAdClicked() {
                        Log.e(TAG, ":onAdClicked ");
                    }

                    @Override
                    public void onAdClosed(View view) {
                        Log.e(TAG, ":onAdClosed ");
                        list.remove(holder.getLayoutPosition());
                        notifyItemRemoved(holder.getLayoutPosition());
                    }

                    @Override
                    public void onRenderFail(View view, String s, int i) {
                        Log.e(TAG, ":onRenderFail ");
                    }

                    @Override
                    public void onRenderSuccess(View view, float v, float v1) {
                        Log.e(TAG, ":onRenderSuccess ");
                        ((ViewGroup) holder.itemView).addView(view);
                    }
                });
                expressInfo.render();
            }

        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class Holder extends RecyclerView.ViewHolder {
        TextView textView;
        ImageView imageView;

        public Holder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.tv_item_native_list);
            imageView = itemView.findViewById(R.id.im_item_native_list);
        }
    }
}
